package com.personal.common.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @description:订单生成规则 年月日时分秒+ 4位随机数
 * @author:     Administrator
 * @date:       2016年1月27日 下午5:29:05 
 * @version:	v1.0
 */
public class OrderIDGenerator {
	
	public static String generatorOrderId(){
		return getCurrTime() + createNoncestr();
	}
	/**
	 * 获取当前时间 yyyyMMddHHmmss
	 * @return String
	 */ 
	private static String getCurrTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}
	private static String createNoncestr() {
		String chars = "0123456789";
		String res = "";
		for (int i = 0; i < 4; i++) {
			Random rd = new Random();
			int index = rd.nextInt(chars.length() - 1);
			res += chars.charAt(index);
		}
		return res;
	}
	public static void main(String[] args) {
		System.out.println(generatorOrderId());
		final Map<String, Object> map = new ConcurrentHashMap<String, Object>();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for (int i = 0; i < 10000; i++) {
					map.put(generatorOrderId(), "");
				}
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				for (int i = 0; i < 10000; i++) {
					map.put(generatorOrderId(), "");
				}
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				while (true) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(map.size());
				}
			}
		}).start();
	}
}
